package com.yc.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.yc.vo.AttendanceData;
import com.yc.vo.Course;
import com.yc.vo.StudentAttendanceRecord;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.*;
import com.yc.entity.Schedule;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 课程表;(schedule)表数据库访问层
 * @date : 2023-6-28
 */
@Mapper
public interface ScheduleMapper  extends BaseMapper<Schedule>{
    /**
     * 分页查询指定行数据
     *
     * @param page 分页参数
     * @param wrapper 动态查询条件
     * @return 分页对象列表
     */

    IPage<Schedule> selectByPage(IPage<Schedule> page , @Param(Constants.WRAPPER) Wrapper<Schedule> wrapper);

    /**
     * 分页查询课程
     * @param course
     * @return
     */
    List<Course> selectCoursePage(@Param("course") Course course);

    /**
     * 查询总行数
     * @param course
     * @return
     */
    int selectTotal(@Param("course") Course course);

    /**
     * 根据日期和教室查询是否有重复的课程
     * @param date
     * @param classrooom
     * @return
     */
    @Select("select count(id) from schedule where  date = #{date} and classroom = #{classroom}")
    Integer selectRepeatSchedule(@Param("date") String date,@Param("classroom") String classrooom);

    /**
     * 根据日期和班级查询日期和班级id
     * @param date
     * @param classid
     * @return
     */
    @Select("select * from schedule where date=#{date} and classId=#{classid}")
    Integer selectDateAndClassId(@Param("date") String date, @Param("classid")String classid);

    /**
     * 根据年月日查询周次
     * @param date
     * @return
     */
    @Select("select week from school_year where time=#{date}")
    Integer selectWeek(String date);

    /**
     *@Description 查询所有课程
     *@Return 所有课程
     *@Author dm
     *@Date Created in 2023/7/11 11:08
     **/
    List<String> queryCourses(@Param("id")String teacherId);

    /**
     * 查询课表ID
     * @param teacher

     * @param classtime
     * @return
     */
    @Select("SELECT id FROM schedule WHERE CONCAT(',', classtime, ',') LIKE CONCAT( '%' ,#{classtime}, '%') and teacherId = #{teacherId} and date like CONCAT(#{date},'%') and classId = #{classId} ")
    Integer querySchId(@Param("teacherId") int teacher,@Param("classtime") String classtime,@Param("date")String date,@Param("classId") int cid);


    /**
     * 查询当天老师给指定班级上课的课表
     * @param username 老师工号
     * @param cid   上课的班级
     * @return  指定班级上课的课表
     */
    Schedule selectScheduleByCidAndDate(@Param("username") String username, @Param("cid")String cid);


    /**
     * 查询课程
     * @param cid 班级编号
     * @return
     */
    Schedule selectScheduleByCid(String cid);


    /**
     * 查询课程和教师
     * @return
     */
    @Select("select coursename,classroom from schedule GROUP BY coursename,classroom")
    List<Schedule> queryCourseNameAndClassroom();

    /**
     * 查询当天老师给班级上课的信息
     * @param teacherId 老师Id
     * @return 上课信息
     */
    List<StudentAttendanceRecord> selectScheduleByTeacherId(@Param("teacherId") String teacherId);

    /**
     * 查询所有的班级
     * @param username 老师的id
     * @return
     */
    List<AttendanceData> selectClassesAll(String username);

    /**
     * 查询考勤表中是否有记录
     * @param username 老师的id
     * @return
     */
    Integer selectRepeat(String username);


    /**
     * 根据班级id和课程名称查询该班级是否存在该课程
     * @param classId 班级id
     * @param courseName 课程名称
     * @return
     */
    @Select("select count(*) from schedule where classId = #{classId} and coursename = #{courseName} " +
            "and classroom =#{classroom} and teacherId = #{teacherId}")
    Integer selectSchedulesByClassIdAndName(@Param("classId") Integer classId,@Param("courseName")
            String courseName,@Param("classroom") String classroom,@Param("teacherId") Integer teacherId);

    /**
     * 根据班级id、课程名称、专业、查询该班级是否存在该课程
     * @param classId 班级id
     * @param courseName 课程名称
     * @return
     */
    @Select("select week from schedule where classId = #{classId} and coursename = #{courseName} " +
            "and classroom =#{classroom} and date = #{date} and teacherId = #{teacherId}")
    Integer selectSchByCidAndNameAndWeek(@Param("teacherId") Integer teacherId,@Param("classId") Integer classId,@Param("courseName")
            String courseName,@Param("classroom") String classroom,@Param("date") String date);

    /**
     * 批量添加课程
     * @param list 课程集合
     * @return
     */
    int addList(List<Schedule> list);

    /**
     * 根据课程名称查询课程,获取课表颜色
     * @param CourseName 课程名称
     * @return 课程
     */
    @Select("select colour from schedule where coursename = #{CourseName} limit 0,1")
    List<Schedule> selectCourseByName(String CourseName);

    /**
     * 单次添加课程
     * @param schedule 课程
     * @return
     */
    int insert(@Param("schedule")Schedule schedule);

}
